<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * Script Name: Addon.php
 * Create: 2020/6/29 15:50
 * Description: 应用公共控制器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace app\common\controller;

class Addon extends BaseCtl
{
    protected $addonName;//应用名称
    protected $addonModule; //模块
    protected $addonController; //控制器
    protected $addonAction;  //操作
    protected $theme; //视图皮肤
    protected $addonInfoByDb;//应用配置已保存的信息
    protected $addonInfoByFile;//应用配置Config文件信息
    protected $addonRoot;//应用的根目录
    protected $addonM;
    protected $mid; //公众号id或小程序id
    protected $mpAddon; //公众号或小程序关联信息

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->setMid();
        $this->addonM = model('common/addons');
        $this->addonName = strtolower($this->request->param('addon'));
        $this->addonModule = strtolower($this->request->param('module'));
        $this->addonController = strtolower($this->request->param('col'));
        $this->addonAction = strtolower($this->request->param('act'));

        $this->setAddonInfo();
        //$this->setMpAddon();
        $this->addonRoot = ADDON_PATH . $this->addonName . '/';
        session('addonName', $this->addonName);
    }

    /**
     * 设置公众号/小程序 ID
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function setMid(){
        $this->mid = input('mid', 0, 'intval');
        /*if(empty($this->mid)){
            $this->error('mid参数错误');
        }*/
    }

    /**
     * 设置公众号(小程序)与应用关联信息
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function setMpAddon(){
        $this->mpAddon = model('common/mpAddon')->getOneByMap(['mpid' => $this->mid, 'addon' => $this->addonName]);
        if(empty($this->mpAddon)){
            $this->mpAddon = model('common/miniAddon')->getOneByMap(['mini_id' => $this->mid, 'addon' => $this->addonName]);
        }
        $this->mpAddon && $this->mpAddon['infos'] = json_decode($this->mpAddon['infos'], true);
    }

    /**
     * 设置应用信息
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function setAddonInfo(){
        $this->addonInfoByDb = $this->addonM->getOneByMap(['addon' => $this->addonName, 'status' => 1]);
        if (empty($this->addonInfoByDb)) {
            $this->error('应用不存在或已下架');
        }
        $this->addonInfoByFile = $this->addonM->getAddonConfigByFile($this->addonName);
    }

    /**
     * 统一视图
     * @param array $vars
     * @param string $template
     * @return mixed|void
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function show($vars = [], $template = '')
    {
        if ($template == null) {
            if (!empty($this->theme)) {
                $template = '@' . $this->theme . '/' . $this->addonAction;
            } else {
                $template = $this->addonAction;
            }
        }
        $_theme = empty($this->theme) ? null : $this->theme;

        $template_pre = ADDON_PATH . $this->addonName . '/' . $this->addonModule . '/view/';
        if (strpos($template, "@") === false) {
            $tpls = explode('/', $template);
            $count = count($tpls);
            $suffix = config('template.view_suffix');
            $isSuffix = false;
            if (!empty($this->theme)) {
                if (count(explode('/', $template)) == 3) {
                    $template = $this->theme . '/' . $template;
                }
            }
            if (count($tpl_suffix = explode('.', $tpls[$count - 1])) == 2) {
                $isSuffix = true;
                if ($suffix != $tpl_suffix[1]) {
                    $suffix = $tpl_suffix[1];
                }
                $suffixLen = strlen($suffix) + 1;
                $template = substr($tpls[$count - 1], 0, -$suffixLen);
            }
            $_theme = $_theme ? $_theme : strtolower($tpls[0]);
            switch ($count) {
                case 1:
                    if (empty($this->theme)) {
                        $_theme = null;
                    } else {
                        $_theme = $_theme ? $_theme : strtolower($tpls[0]);
                    }
                    $template = $template_pre . $_theme . '/' . $this->addonController . '/' . $template . '.' . $suffix;
                    break;
                case 2:
                    if (!empty($tpls[0]) && $isSuffix == true) {
                        $template = $template_pre . $_theme . '/' . $template . '.' . $suffix;
                    } else {
                        if (!empty($this->theme)) {
                            $_theme = $_theme . '/' . strtolower($tpls[0]);
                        }
                        $template = $template_pre . $_theme . '/' . $tpls[1] . '.' . $suffix;
                    }
                    break;
            }
        } else {
            $template = substr($template, 1);
            $tpls = explode('/', $template);
            $count = count($tpls);
            $suffix = config('template.view_suffix');
            $isSuffix = false;
            if (count($tpl_suffix = explode('.', $tpls[$count - 1])) == 2) {
                $isSuffix = true;
                if ($suffix != $tpl_suffix[1]) {
                    $suffix = $tpl_suffix[1];
                }
                $suffixLen = strlen($suffix) + 1;
                $template = substr($tpls[$count - 1], 0, -$suffixLen);
            }
            $_theme = $_theme ? $_theme : strtolower($tpls[0]);
            switch ($count) {
                case 2:
                    $template = $template_pre . $_theme . '/' . $this->addonController . '/' . ($isSuffix == true ? $template : $tpls[1]) . '.' . $suffix;
                    break;
                case 3:
                    $template = $template_pre . $_theme . '/' . strtolower($tpls[1]) . '/' . ($isSuffix == true ? $template : $tpls[2]) . '.' . $suffix;
                    break;
            }
        }
        $config['view_path'] = $template_pre;
        $this->assign['addonInfo'] = $this->addonInfoByDb;
        $this->assign['module'] = $this->addonModule;
        $this->assign['controller'] = $this->addonController;
        $this->assign['action'] = $this->addonAction;
        $vars = array_merge($this->assign, $vars);
        return parent::fetch($template, $vars, $config); // TODO: Change the autogenerated stub
    }
}